<?php

namespace app\model\store;

use app\base\BaseModel;
use app\model\user\UserModel;
use app\model\goods\GoodsModel;
use think\model\concern\SoftDelete;

class StoreModel extends BaseModel
{
    use SoftDelete;

    protected $name = 'store';

    /**
     * 模型关联-StoreGradeModel
     *
     * @return void
     */
    public function grade()
    {
        return $this->hasOne(StoreGradeModel::class, 'id', 'grade_id')->bind(['grade_name' => 'name']);
    }

    /**
     * 模型关联-UserModel
     *
     * @return void
     */
    public function user()
    {
        return $this->hasOne(UserModel::class, 'id', 'user_id')->bind(['user_name' => 'nick_name']);
    }

    /**
     * 获取器-store_type
     *
     * @param [type] $value
     * @return void
     */
    public function getStoreTypeTextAttr($value, $data)
    {
        $arr = [0 => '个人', 1 => '企业'];
        return $arr[$data['store_type']];
    }

    /**
     * 获取器-store_status
     *
     * @param [type] $value
     * @return void
     */
    public function getStoreStatusTextAttr($value, $data)
    {
        $arr = [0 => '关闭', 1 => '开启'];
        return $arr[$data['store_status']];
    }

    /**
     * 获取器-check_status
     *
     * @param [type] $value
     * @return void
     */
    public function getCheckStatusTextAttr($value, $data)
    {
        $arr = [0 => '审核中', 1 => '已通过', 2 => '未通过'];
        return $arr[$data['check_status']];
    }

    /**
     * 获取器-is_own_shop
     *
     * @param [type] $value
     * @return void
     */
    public function getIsOwnShopTextAttr($value, $data)
    {
        $arr = [0 => '非自营店', 1 => '自营店'];
        return $arr[$data['is_own_shop']];
    }

    /**
     * 范围查询-keyword
     *
     * @param [type] $query
     * @param [type] $keyword
     * @return void
     */
    public function scopeKeyword($query, $keyword)
    {
        if (!empty($keyword)) {
            $query->whereLike('store_name', "%{$keyword}%");
        }
    }

    /**
     * 范围查询-store_status
     *
     * @param [type] $query
     * @param [type] $store_status
     * @return void
     */
    public function scopeStoreStatus($query, $store_status)
    {
        if (!empty($store_status) || is_numeric($store_status)) {
            $query->where('store_status', $store_status);
        }
    }

    /**
     * 范围查询-check_status
     *
     * @param [type] $query
     * @param [type] $check_status
     * @return void
     */
    public function scopeCheckStatus($query, $check_status)
    {
        if (!empty($check_status) || is_numeric($check_status)) {
            if (intval($check_status) == -1) {
                $query->whereIn('check_status', [0, 2]);
            } else {
                $query->where('check_status', $check_status);
            }

        }
    }

    /**
     * 格式化数据
     * @param $data
     */
    public static function formatx(&$data)
    {
        $data['store_deposit'] = floatval($data['store_deposit']);
        $type_tag = [];
        if (intval($data['is_own_shop']) == 1) {
            $type_tag[] = [
                'name' => "直营店"
            ];
        }
        if (intval($data['store_type']) == 1) {
            $type_tag[] = [
                'name' => "企业店铺"
            ];
        } else {
            $type_tag[] = [
                'name' => "个体经营"
            ];
        }
        $data['type_tag'] = $type_tag;
        $data['time'] = date("Y-m-d", strtotime($data['create_time']));
    }

    /**
     * 获取商家分页
     *
     * @param [type] $keyword
     * @param [type] $status
     * @param [type] $current_page
     * @param [type] $list_rows
     * @return void
     */
    public function getPageData($keyword, $store_status, $check_status, $current_page, $list_rows)
    {
        $dataList = $this
            ->with(['grade', 'user'])
            ->alias('a')
            ->field('a.id,a.grade_id,a.user_id,a.store_name,a.store_type,a.store_status,a.check_status,a.store_deposit,a.store_label,
            a.store_credit,a.store_desccredit,a.store_servicecredit,a.store_deliverycredit,
            a.store_collect,a.store_sales,a.is_own_shop,a.real_name,a.contact_phone,a.sort,a.create_time')
            ->keyword($keyword)
            ->storeStatus($store_status)
            ->checkStatus($check_status)
            ->append(['store_type_text', 'store_status_text', 'check_status_text', 'is_own_shop_text'])
            ->paginate(['page' => $current_page, 'list_rows' => $list_rows], false);
        $current = $dataList->currentPage();
        $total = $dataList->total();
        $limit = $dataList->listRows();
        $items = $dataList->items();
        $ret = ['total' => $total, 'current' => $current, 'limit' => $limit, 'dataList' => $items];
        return $ret;
    }

    /**
     * 获取商家信息
     * @param $id
     * @param $field
     * @return array|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getStoreInfo($id, $field)
    {
        $store = self::where('id', $id)->field($field)->find();
        return $store;
    }

    /**
     * 获取自营店铺id集合
     *
     * @return void
     */
    public function getStoreIdsByOwnShop()
    {
        $store_ids = $this->where('is_own_shop', 1)->column('id');
        return $store_ids;
    }

    /**
     * 获取商家商品
     *
     * @return void
     */
    public function getGoodsByStore()
    {
        $db_goods = new GoodsModel();
        $data = $this->field('id,store_name')->storeStatus(1)->checkStatus(1)->order('sort desc')->select();
        foreach ($data as $obj) {
            $obj['goods'] = $db_goods->field('id,goods_img')->where('store_id', $obj['id'])->where('goods_status', 1)->order('sort desc')->limit(3)->select();
        }
        return $data;
    }
}   